/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.ConnectionPointReference;
import org.eclipse.uml2.uml.FinalState;
import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.uml2.uml.PseudostateKind;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagram.statemachine.helper.NodePseudostateHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Property;

/**
 * Manages presentation section of Pseudostates and FinalStates.
 *
 * @author David Servat
 * @since 11 apr. 07
 */
public class PseudostatePresentationSection extends PropertyViewSection{

	/** The check box show name. */
	private Button checkBoxShowName;
		
	/** The show name listener. */
	private SelectionListener showName;
	
	/** The check box horizontal. */
	private Button checkBoxHorizontal;	
		
	/** The check box vertical. */
	private Button checkBoxVertical;
			
	/** The show display listener. */
	private SelectionListener showDisplay;
	
	/**
	 * Creates the controls.
	 * 
	 * @param tabbedPropertySheetPage the tabbed property sheet page
	 * @param parent the parent
	 */
	public void createControls(final Composite parent, final TabbedPropertySheetPage tabbedPropertySheetPage){
		super.createControls(parent, tabbedPropertySheetPage);
		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
		
		//show name
		checkBoxShowName = this.getWidgetFactory().createButton(composite, "Show Name", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 100);
		data.top = new FormAttachment(0, 0);
		checkBoxShowName.setLayoutData(data);
		
		showName = new SelectionListener(){
			/**
			 * Widget default selected.
			 * 
			 * @param e the e
			 */
			public void widgetDefaultSelected(SelectionEvent e){}

			/**
			 * Widget selected.
			 * 
			 * @param e the e
			 */
			public void widgetSelected(SelectionEvent e){
				if((umlElement instanceof Pseudostate) || (umlElement instanceof FinalState) || (umlElement instanceof ConnectionPointReference)){
					if((diagramElement != null) && (diagramElement instanceof GraphElement)){
						((GraphNode)((GraphElement)diagramElement).getSemanticChild().get(0)).setIsVisible(!((GraphNode)((GraphElement)diagramElement).getSemanticChild().get(0)).isIsVisible());
						forceToSave();
					}		
				}
			}
		};
		
		checkBoxShowName.addSelectionListener(showName);
			
		//horizontal
		checkBoxHorizontal = this.getWidgetFactory().createButton(composite, "Horizontal", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxShowName, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxHorizontal.setLayoutData(data);
		checkBoxHorizontal.setEnabled(false);
		
		//vertical
		checkBoxVertical = this.getWidgetFactory().createButton(composite, "Vertical", SWT.RADIO);
		data = new FormData();
		data.left = new FormAttachment(checkBoxHorizontal, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxVertical.setLayoutData(data);
		checkBoxVertical.setEnabled(false);
		
		showDisplay = new SelectionListener(){
			/**
			 * Widget default selected.
			 * 
			 * @param e the e
			 */
			public void widgetDefaultSelected(SelectionEvent e){}

			/**
			 * Widget selected.
			 * 
			 * @param e the e
			 */
			public void widgetSelected(SelectionEvent e){
				if(umlElement instanceof Pseudostate){
					if((diagramElement != null) && (diagramElement instanceof GraphElement)){
						Property prop = ((GraphElement)diagramElement).getProperty(NodePseudostateHelper.PSEUDOSTATE_DISPLAY);
						if(prop != null){
							if(e.getSource().equals(checkBoxHorizontal)){
								if(checkBoxHorizontal.getSelection()){
									checkBoxVertical.setSelection(false);
									prop.setValue(NodePseudostateHelper.PSEUDOSTATE_DISPLAY_HORIZONTAL);
								}
								else{
									checkBoxVertical.setSelection(true);
									prop.setValue(NodePseudostateHelper.PSEUDOSTATE_DISPLAY_VERTICAL);									
								}
							}
							else if(e.getSource().equals(checkBoxVertical)){
								if(checkBoxVertical.getSelection()){
									checkBoxHorizontal.setSelection(false);
									prop.setValue(NodePseudostateHelper.PSEUDOSTATE_DISPLAY_VERTICAL);
								}
								else{
									checkBoxHorizontal.setSelection(true);
									prop.setValue(NodePseudostateHelper.PSEUDOSTATE_DISPLAY_HORIZONTAL);									
								}
							}
							forceToSave();
						}	
					}		
				}
			}
		};

		checkBoxHorizontal.addSelectionListener(showDisplay);
		checkBoxVertical.addSelectionListener(showDisplay);
		
		//label of the section
		CLabel pseudostatePresentationLabel = this.getWidgetFactory().createCLabel(composite, "Display:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(checkBoxShowName, -ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(checkBoxShowName, 0, SWT.CENTER);
		pseudostatePresentationLabel.setLayoutData(data);
	}

	/**
	 * Refreshes the view.
	 */
	@Override
	public void refresh(){
		if(!checkBoxShowName.isDisposed() && !checkBoxHorizontal.isDisposed() && !checkBoxVertical.isDisposed()){
			//remove all listeners
			checkBoxShowName.removeSelectionListener(showName);
			checkBoxHorizontal.removeSelectionListener(showDisplay);
			checkBoxVertical.removeSelectionListener(showDisplay);
			
			if((diagramElement != null) && (diagramElement instanceof GraphElement) && (((GraphElement)diagramElement).getSemanticChild() != null) && (((GraphElement)diagramElement).getSemanticModel() != null)){
				if(umlElement instanceof Pseudostate){
					checkBoxShowName.setEnabled(true);
					checkBoxShowName.setSelection(((GraphNode)((GraphElement)diagramElement).getSemanticChild().get(0)).isIsVisible());

					if(((Pseudostate)umlElement).getKind().equals(PseudostateKind.JOIN_LITERAL) || ((Pseudostate)umlElement).getKind().equals(PseudostateKind.FORK_LITERAL)){
						checkBoxHorizontal.setEnabled(true);
						checkBoxVertical.setEnabled(true);
						Property prop = ((GraphElement)diagramElement).getProperty(NodePseudostateHelper.PSEUDOSTATE_DISPLAY);
						if(prop.getValue().equals(NodePseudostateHelper.PSEUDOSTATE_DISPLAY_HORIZONTAL)){
							checkBoxHorizontal.setSelection(true);
							checkBoxVertical.setSelection(false);
						}
						else{
							checkBoxHorizontal.setSelection(false);
							checkBoxVertical.setSelection(true);								
						}
					}
					//other types of Pseudostates
					else{
						checkBoxHorizontal.setVisible(false);
						checkBoxVertical.setVisible(false);
					}			
				}
				else if((umlElement instanceof FinalState) || (umlElement instanceof ConnectionPointReference)){
						checkBoxShowName.setEnabled(true);
						checkBoxShowName.setSelection(((GraphNode)((GraphElement)diagramElement).getSemanticChild().get(0)).isIsVisible());
						
						checkBoxHorizontal.setVisible(false);
						checkBoxVertical.setVisible(false);
				}
			}
			else{
				checkBoxShowName.setVisible(false);
				checkBoxHorizontal.setVisible(false);
				checkBoxVertical.setVisible(false);
			}			
			checkBoxShowName.addSelectionListener(showName);
			checkBoxHorizontal.addSelectionListener(showDisplay);
			checkBoxVertical.addSelectionListener(showDisplay);
		}		
	}
	
	/**
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if((checkBoxShowName != null) && !checkBoxShowName.isDisposed())
			checkBoxShowName.removeSelectionListener(showName);
		if((checkBoxHorizontal != null) && !checkBoxHorizontal.isDisposed())
			checkBoxHorizontal.removeSelectionListener(showDisplay);
		if((checkBoxVertical != null) && !checkBoxVertical.isDisposed())
			checkBoxVertical.removeSelectionListener(showDisplay);
	}
}